const [y, x, z] = [
  ['img/scissors.png', 'img/stone.png', 'img/cloth.png', 'img/pre.png'],
  [0.33, 0.67],
  [
    '真棒，少了一个病毒呢，蕾姆酱越来越安全啦',
    '啊，又多了一个病毒！蕾姆酱害怕！',
    '病毒还是这么多呢，蕾姆害怕，尼酱',
    '',
  ],
];

let SumTimes = 0;
let Sumsuss = 0;
let Sumfail = 0;
let Sumtied = 0;

// 结果统计
function Add(ab) {
  const [suma, sumb, sumc, sumd] = [
    document.getElementById('suma'),
    document.getElementById('sumb'),
    document.getElementById('sumc'),
    document.getElementById('sumd'),
  ];
  const virusGroup = document.querySelector('#virusGroup');
  const virus = document.querySelector('.virus');

  SumTimes++;

  if (ab == '02' || ab == '10' || ab == '21') {
    Sumsuss++;
    suma.innerHTML = Sumsuss;
    virusGroup.removeChild(virus);
    if (!document.querySelectorAll('.virus').length) {
      alert('尼酱真棒呢！蕾姆重获健康之身啦！');
    }
  }

  if (ab == '00' || ab == '11' || ab == '22') {
    Sumfail++;
    sumb.innerHTML = Sumfail;
  }

  if (ab == '01' || ab == '12' || ab == '20') {
    const img = document.createElement('img');
    img.classList.add('virus');
    img.src = './img/virus.png';
    if (document.querySelectorAll('.virus').length > 10) {
      alert('病毒太多啦，蕾姆害怕尼酱');
    }
    Sumtied++;
    sumc.innerHTML = Sumtied;
    virusGroup.appendChild(img);
  }

  const rate = (Sumsuss / SumTimes) * 100;
  sumd.innerHTML = `${String(rate.toFixed(2))}%`;
}

function Act(nSrc, aNum, bNum, cNum, d) {
  // @param
  // nSrc: 玩家图片索引
  // aNum, bNum, cNum: 结果索引
  const [computer, result, person, rd] = [
    document.getElementById('computer'),
    document.getElementById('result'),
    document.getElementById('person'),
    Math.random(),
  ];

  person.src = y[nSrc];

  if (rd < x[0]) {
    computer.src = y[0];
    result.innerHTML = z[aNum];
    Add(String(nSrc) + String(0));
  } else if (rd < x[1]) {
    computer.src = y[1];
    result.innerHTML = z[bNum];
    Add(String(nSrc) + String(1));
  } else {
    computer.src = y[2];
    result.innerHTML = z[cNum];
    Add(String(nSrc) + String(2));
  }
}

// 主函数
function mora() {
  const [s, st, c, info, close] = [
    document.getElementById('s'),
    document.getElementById('st'),
    document.getElementById('c'),
    document.getElementById('info'),
    document.getElementById('close'),
  ];

  info.style.display = 'block';

  close.onclick = () => {
    info.style.display = 'none';
  };

  s.onclick = () => {
    Act(0, 2, 1, 0);
  };

  st.onclick = () => {
    Act(1, 0, 2, 1);
  };

  c.onclick = () => {
    Act(2, 1, 0, 2);
  };
}
